/*************************************************
 * CRIEP.java                                    *
 * The main class in which all GUI is created    *
 *************************************************/
package criep;

//Imports
import arena.Arena;
import graphics.Point;
import gui.Client;
import gui.Main;
import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.UIManager;
import robot.DBComm;
import robot.PhysicalRobot;

/**
 * This is the main class which creates the GUI and retrieves the data from
 * the database regarding the physical robots.
 *
 * @author      Gabriel Loewen
 * @author      James Weston
 * @author      Jackie O'Quinn
 */
public class CRIEP {

    //Define an arena for the virtual robots and for the physical robots
    public static Arena VirtualArena = new Arena(Client.virtualWidth, Client.virtualHeight);
    public static Arena PhysicalArena = new Arena(6, 6);
    //The frame which will contain the main client panel
    public static JFrame frame = new JFrame();

    public static void main(String[] args) {

        String osName = System.getProperty("os.name");
        try {
            if (osName.equalsIgnoreCase("linux")) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
            } else if (osName.equalsIgnoreCase("windows")) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            } else if (osName.equalsIgnoreCase("mac os x")) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.mac.MacLookAndFeel");
            }
        } catch (Exception ex) {
        }

        //Set options for the frame
        frame.setTitle("Developmental Robotics Lab - CRIEP Client");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(640, 480));
        frame.setSize(new Dimension(800, 600));
        frame.setLocationRelativeTo(null);
        frame.add(new Main());
        frame.setVisible(true);
        frame.setResizable(false);

    }

    /**
     * Retrieves the robots from the database and populates the robot list
     * as well as repainting the grid to show the robots initial positions.
     */
    public static void getRobotsFromDB() {
        int timeout = 1000;
        while (timeout > 0 && !DBComm.isConnected()) {
            try {
                timeout--;
                Thread.sleep(1);
            } catch (InterruptedException ex) {
            }
        }
        if (!DBComm.isConnected()) {
            Client.updateOutput("Unable to connect to database!  Try again.");
            return;
        }
        try {
            int robotCount = DBComm.getRobotCount();
            Client.updateOutput("Connecting to database...");
            Client.updateOutput("Retrieving robots");
            DefaultListModel robotList = new DefaultListModel();
            PhysicalArena.robotMap.clear();
            for (int i = 1; i <= robotCount; i++) {
                String id = "Robot" + i;
                int[] data = DBComm.read(id);
                if (data != null) {
                    PhysicalRobot robot = new PhysicalRobot(id, new Point(data[1], data[2]), (char) data[0], data[3], data[4], data[5]);
                    PhysicalArena.robotMap.put(id, robot);
                    robotList.addElement(id);
                }
            }
            Client.lstPhysicalRobots.setModel(robotList);
            Client.updateOutput("...Done. " + robotCount + " robots created.");
            PhysicalArena.getGrid().repaint();
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }
    }
}
